/*
 * @Description: 
 * @Version: 2.0
 * @Autor: lxc
 * @Date: 2020-12-15 13:45:57
 * @LastEditors: lxc
 * @LastEditTime: 2020-12-16 17:39:45
 */
const Validator = require("validator");
const isEmpty = require('./is-empty');

module.exports = function validateRegisterInput(data) {
    let errors = {};

    data.name = !isEmpty(data.name) ? data.name : '';
    data.email = !isEmpty(data.email) ? data.email : '';
    data.password = !isEmpty(data.password) ? data.password : '';
    data.password2 = !isEmpty(data.password2) ? data.password2 : '';

    if (!Validator.isLength(data.name, { min: 2, max: 30 })) {
        errors.msg = "名字长度不能小于2位且不能大于30位"
    } else if (Validator.isEmpty(data.name)) {
        errors.msg = "名字不能为空！"
    } else if (Validator.isEmpty(data.email)) {
        errors.msg = "邮箱不能为空！"
    } else if (!Validator.isEmail(data.email)) {
        errors.msg = "邮箱不合法！"
    } else if (Validator.isEmpty(data.password)) {
        errors.msg = "密码不能为空！"
    } else if (!Validator.isLength(data.password, { min: 6, max: 30 })) {
        errors.msg = "名字长度不能小于6位且不能大于30位"
    } else if (Validator.isEmpty(data.password2)) {
        errors.msg = "确认密码不能为空！"
    } else if (!Validator.equals(data.password, data.password2)) {
        errors.msg = '两次密码不一致';
    }

    return {
        errors,
        isValid: isEmpty(errors)
    };
}